define([
  'jquery',
  'jqueryui',
  'underscore',
  'backbone',
  'models/history/PatientHistoryModel',
  'text!templates/history/patientHistoryTemplate.html',
  'msgbox',
  'multiselect'
], function($,JQueryUI, _, Backbone, PatientHistoryModel, patientHistoryTemplate,MsgBox,Multiselect){

  var PatientHistoryView = Backbone.View.extend({
    el: $(".mainpanel"),
    events: {'click #submitphistory' : 'submitphButton',
        "change #patienthistoryform :input" :"changedHistory",
        "change #patienthistoryform textarea" :"changedHistory"
        } ,
     model:  new PatientHistoryModel({id: 0}),
    initialize: function(){
    	//_.bindAll(this, "changed");
    },
    render: function(){
        var lOptions = {};
        var self=this;
        lOptions.success = function(){
            var data = {
          		  history: self.model,
          	        _: _ 
          	      };
   		      self.loadTemplate(data);

        };
        this.model = new PatientHistoryModel({id: 0});
        this.model.fetch(lOptions);

    },
    loadTemplate: function(data){
	      var compiledTemplate = _.template( patientHistoryTemplate, data );
  	      this.$el.html(compiledTemplate);
  	  $("#maritalStatus").multiselect({
  		   multiple: false,
  		   header: "Select an option",
  		   noneSelectedText: "Select an Option",
  		   selectedList: 1
  		});
  	  $("#gender").multiselect({
 		   multiple: false,
 		   header: "Select an option",
 		   noneSelectedText: "Select an Option",
 		   selectedList: 1
 		});

    },
    submitphButton: function(){
    	console.log(this.model);
       	this.model.save(null, {url:"/patientHistory",
		    success: function (model, response) {
	      		 $.msgBox({
				    title:"Appment",
				    content:"Successfully Saved!",
				    type:"info",
				    success: function (result) {}
				});

		    },
		    error: function (model, response) {
		        console.log("error");
		    }
		});

    },
    changedHistory:function(evt) {
    	//console.log(evt);   
       var changed = evt.currentTarget;
       var value = $("#"+changed.id).val();
       if($('#'+changed.id).is(':checkbox')==true){
    	   value=value=="on"?"Y":"N"
       }
       var obj = "{\""+changed.id +"\":\""+value+"\"}";
       var objInst = JSON.parse(obj);
       this.model.set(objInst);
    }
  });

  return PatientHistoryView;
});
